﻿"use strict";

window.ArthurGraus = window.ArthurGraus || {};

var youTubeAppPart;

$(document).ready(function () {
    youTubeAppPart = new ArthurGraus.YouTubeAppPart();
    youTubeAppPart.initialize();
});

ArthurGraus.YouTubeAppPart = function () {
    var self = this;
    self.videoFormatWidth = 0;
    self.videoFormatHeigth = 0;
    self.youTubeVideoCode = "";
    self.analytics = null;

    self.initialize = function () {
        self.analytics = new ArthurGraus.Analytics('YOTU');
        self.analytics.initialize();

        var video = decodeURIComponent(getQuerystring("VideoFormat"));

        self.videoFormatWidth = parseInt(video);
        self.videoFormatHeigth = Math.round(self.videoFormatWidth / 1.777777777);
 
        var youTubeUrl = decodeURIComponent(getQuerystring("YouTubeUrl"));

        self.youTubeVideoCode = getQuerystringFromString(youTubeUrl, "v");

        var frameHTML = '<iframe width="{width}" height="{height}" src="//www.youtube.com/embed/{code}" frameborder="0" allowfullscreen></iframe>';

        frameHTML = frameHTML.replace("{width}", self.videoFormatWidth);
        frameHTML = frameHTML.replace("{height}", self.videoFormatHeigth);
        frameHTML = frameHTML.replace("{code}", self.youTubeVideoCode);

        $("#youTubeFrame").html(frameHTML);
    };


};

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}
function getQuerystringFromString(url, key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(url);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function htmlEscape(str) {
    return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;')
            .replace(/ /g, '&nbsp;')
            .replace(/\n/g, '<br />');
}





